/**
 * @class   BigBrotherImpl
 * @brief   
 *
 *
 *
 *
 *
 ****************************************************************************
 * @version $Id: BigBrotherImpl.h 2887 2010-08-11 08:09:41Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Services_BigBrotherImpl_H__
 #define __WOSH_Services_BigBrotherImpl_H__

 #include <core/BundleGenericWorker.h>
 #include <core/Map.h>
 #include <core/Table.h>
 #include <core/MessageQueue.h>
 #include <core/Notification.h>

 #include <framework/building/Location.h>
 #include <interfaces/services/CommunicatorService.h>

 #include "IBigBrotherListener.h"


using namespace wosh;
using namespace wosh::building;


class BigBrotherImpl : public BundleGenericWorker  {

	public:

		/**
		 * @brief   Default constructor. Init vars.
		 * @param bundle [in] the parent bundle, see BundleGenericWorker interface
		 */
		BigBrotherImpl( BundleGeneric& bundle );

		/**
		 * @brief   Deconstructor. If thread is stll running, it will be stopped.
		 */
		virtual ~BigBrotherImpl();


	public:

		void evalMessageIncoming( const Message& message );

		void evalResponse( const Message& message );

		void evalRooms( const Table* childrenprops, const URI& uri );
		void evalDoors( const Table* childrenprops, const URI& uri );

		void evalRoom( const Table* children, const URI& uri );

		void evalDevices( const Table* listing, const URI& uri );

		void evalDoor( const Table* props, const URI& uri );

		void evalDevice_( const string& dev_uri, const string& dev_interfaces, const URI& uri );


		void setUserLocation( const std::string& username, const string& new_location, double probability );
		void setUserLocation_( const std::string& username, Location* hLoc, double probability );

	public:

	//	WRESULT setUserInLocation( const string& location, const string& user );

	//	string getUserLocation( const string& user ) const;


//@}
/** @name Setters
 * @{
 ******************************************************************************/
	public:

		void setListener( IBigBrotherListener* list )	{ this->listener = list; }

//@}
/** @name Getters
 * @{
 ******************************************************************************/
	public:

		Map<string, Location*>& getLocations()			{ return this->locations; }

	//	Map<string, UserPosition*>& getUserPositionDB()		{ return this->users; }


//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:

		void evalMessage( Message* message );

		Location* getLocationByURI_( const URI& uri );
		Location* getLocationParent_( const Location* child );


		void evalTimeOut();

		void recognizeUser_( bool userFound, Location* cLocation, Message* message );

		bool recognizeUserAppearedIn_( Location* cLocation, Message* message );
		bool recognizeUserDisappearedIn_( Location* cLocation, Message* message );


	protected:

		void updateSubItem_( const string& parent_bobj, const string& type, const URI& uri );

		void updatePlayerAudio_( const string& parent, const URI& uri );
		void updateSensorMotion_( const string& parent, const URI& uri );


		/**
		 * @brief   
		 * @see     running
		 * @see     
		 */
		virtual void runThread();

//@}


		mutable Map<string, string> communicatorsDevices;	///< uri, loc-name
		mutable Map<string, Location*> locations;		///< name, loc*

		MessageQueue msgQueue;				///< messages' queue

		IBigBrotherListener* listener;		///< callbacks


}; // class def


#endif //__WOSH_Services_BigBrotherImpl_H__
